package com.example.su_shi_tan_wallet

import android.text.Editable
import android.text.SpannableString
import android.view.View
import android.widget.Toast
import androidx.appcompat.widget.Toolbar
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.alibaba.fastjson.JSON
import com.example.library_common.utils.normalShortToast
import com.example.su_shi_tan_bas_lib.base.BaseFragmentEx
import com.example.su_shi_tan_bas_lib.base.BaseLazyDataBindingFragment
import com.example.su_shi_tan_bas_lib.networking.RestClient
import com.example.su_shi_tan_bas_lib.utils.OnTextListener
import com.example.su_shi_tan_core.arouter.AppARouter
import com.example.su_shi_tan_core.arouter.AppARouterParams
import com.example.su_shi_tan_core.arouter.WalletARouter
import com.example.su_shi_tan_core.arouter.WalletARouterParams
import com.example.su_shi_tan_core.db.UserInfoBeanHolder
import com.example.su_shi_tan_core.extentions.castBaseFragmentEx
import com.example.su_shi_tan_core.extentions.startNavigation
import com.example.su_shi_tan_core.getUserInfo

import com.example.su_shi_tan_core.utils.HiDataBus
import com.example.su_shi_tan_core.utils.PayingPassWordVerifyPop
import com.example.su_shi_tan_wallet.databinding.TransferFragmentBinding
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.interfaces.SimpleCallback
import com.senon.modularapp.util.JssSpannableString

/**
 * 转账
 * */

@Route(path = WalletARouter.TransferFragment)
class TransferFragment : BaseLazyDataBindingFragment<TransferFragmentBinding>(), OnTextListener,
    View.OnClickListener {

    @JvmField
    @Autowired(name = WalletARouterParams.tokenType)
    var tokenType: String = ""

    //数量
    private var mCount = ""

    //地址
    private var address = ""

    override fun layoutId(): Int {
        return R.layout.transfer_fragment
    }

    override fun lazyInit() {
        HiDataBus.with<String>(AppARouterParams.ScanFrameParams)
            .observerSticky(this, false, {
                viewBinder.addressTv.setText(it)
            })
    }

    override fun initView(view: View) {

        val mToolbar: Toolbar = view.findViewById(R.id.mToolbar)
        mToolbar.title = "转账"
        mToolbar.setNavigationOnClickListener { onBackPressedSupport() }

        viewBinder.onTextListener = this
        viewBinder.onViewClick = this
        viewBinder.moneyTv.text = tokenType

        showMoney()
        loadRootFragment(
            R.id.wallet_header,
            ARouter.getInstance()
                .build(WalletARouter.WalletHeaderFragment)
                .withString(WalletARouterParams.tokenType, tokenType)
                .castBaseFragmentEx()
        )

    }

    private fun showMoney() {

        when (tokenType) {
            WalletARouterParams.LD -> {
                val integral = UserInfoBeanHolder.getUserInfoBean()?.integral ?: ""

                viewBinder.feedingTv.text = JssSpannableString(
                    _mActivity,
                    "可用${integral}$tokenType"
                )
                    .first(integral)
                    .textColor(R.color.PrimaryColor)
            }
            WalletARouterParams.SST -> {

            }
            WalletARouterParams.FVC -> {

            }
            WalletARouterParams.TD -> {

            }
        }


    }

    override fun afterTextChanged(s: Editable?, viewId: Int) {
        when (viewId) {
            R.id.addressTv -> {
                address = s.toString()
            }
            R.id.availableCount -> {
                mCount = s.toString()
            }
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.mSaner -> {
                ARouter.getInstance()
                    .build(AppARouter.ScanFrameFragment)
                    .startNavigation(this)
            }
            R.id.forAll -> {
                val integral = UserInfoBeanHolder.getUserInfoBean()?.integral
                viewBinder.availableCount.setText("$integral")
            }
            R.id.sureTv -> {
                if (!checkInfo()) {
                    return
                }
                val mPop = PayingPassWordVerifyPop(_mActivity)
                XPopup.Builder(_mActivity)
                    .setPopupCallback(object : SimpleCallback() {
                        override fun onDismiss(popupView: BasePopupView?) {
                            super.onDismiss(popupView)
                            if (mPop.getIsOnVerifyPop()) {
                                if (mPop.getVerifyPop()) {
                                    sendAll()
                                } else {
                                    normalShortToast(_mActivity,"支付密码错误")
                                }
                            }
                        }
                    })
                    .asCustom(mPop)
                    .show()


            }
        }
    }


    private fun checkInfo(): Boolean {
        var flag = true

        if (address.isNullOrBlank()) {
            normalShortToast(_mActivity,"请输入转账地址")

            flag = false
        }

        if (mCount.isNullOrBlank()) {
            normalShortToast(_mActivity,"请输入转账地址数量")

            flag = false
        }

        return flag
    }


    private fun sendAll() {
        RestClient.builder()
            .url(R.string.user_transfer)
            .raw(
                JSON.toJSONString(
                    mapOf(
                        "to_addr" to address,
                        "type" to tokenType,
                        "num" to mCount
                    )
                )
            )
            .success {
                normalShortToast(_mActivity, "转账成功")
                getUserInfo(_mActivity)
                onBackPressedSupport()
            }
            .failure {
                normalShortToast(_mActivity, R.string.net_work_failed)
            }.error { _, msg ->
                normalShortToast(_mActivity, msg)
            }.build().post()
    }

    override fun onBackPressedSupport(): Boolean {
        pop()
        return true
    }


}